﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace QqForm
{
    /// <summary>
    /// TcpClient连接器
    /// </summary>
    public class TcpClass
    {
        #region 字段属性
        public TcpClient TcpClient { get; set; }

        public bool IsExit { get; set; }

        private NetworkStream networkStream;

        private BinaryReader binaryReader;
        private BinaryWriter binaryWriter;

        public delegate void MessageHelper(byte[] msg);

        public event MessageHelper messageHelper;


        private Thread thread;
        #endregion

        public TcpClass()
        {
            IsExit = false;
        }


        #region 创建TCP链接
        /// <summary>
        /// 创建TCP链接
        /// </summary>
        /// <returns></returns>
        public bool Connection()
        {
            this.IsExit = true;
            try
            {
                TcpClient = new TcpClient("211.136.236.88", 14000);
            }
            catch
            {
                return false;
            }
            networkStream = TcpClient.GetStream();
            binaryReader = new BinaryReader(networkStream, Encoding.UTF8);
            binaryWriter = new BinaryWriter(networkStream, Encoding.UTF8);

            thread = new Thread(GetData);
            thread.IsBackground = true;
            thread.Start();
            return true;
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据
        /// </summary>
        private void GetData()
        {
            while (IsExit)
            {
                byte[] message = new byte[100000];
                try
                {
                    int i = binaryReader.Read(message, 0, message.Length);
                    messageHelper(message);
                }
                catch (Exception)
                {
                    IsExit = false;
                    throw;
                }
            }
        }
        #endregion

        #region 发送数据
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="data">需要发送的数据</param>
        public void Send(string data)
        {
            try
            {
                binaryWriter.Write(data);
                binaryWriter.Flush();
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion

        #region 发送数据
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="data">需要发送的数据</param>
        public void Send(byte[] data)
        {
            try
            {
                binaryWriter.Write(data);
                binaryWriter.Flush();
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion

        #region 关闭TCP链接
        /// <summary>
        /// 关闭TCP链接
        /// </summary>
        public void Close()
        {
            try
            {
                if (binaryReader != null)
                {
                    binaryReader.Close();
                }
                if (binaryWriter != null)
                {
                    binaryWriter.Close();
                }
                if (TcpClient != null)
                {
                    TcpClient.Close();
                }
                TcpClient = null;
                IsExit = false;
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        #endregion

        #region 检测是否掉线
        /// <summary>
        /// 检测是否掉线
        /// </summary>
        public void IsConnection()
        {
            TcpClient.Client.Close();
            TcpClient.Close();
            TcpClient = null;
        }
        #endregion
    }
}
